// pages/spray/analys.js
const app = getApp()
import * as echarts from '../../ec-canvas/echarts';
const utils = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    skin: '',
    ec: {
      lazyLoad: true
    },
    deviceId : ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.setNavBar()
    const { skin } = app.globalData
    this.setData({
      skin,
      deviceId : options.deviceId || 0
    })
    this.getMockData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 模拟数据
  getMockData() {
    this.calEleOpen();
  },

  calEleOpen: function () {
    let that = this;
    let threeDay = utils.getDay(-7), 
      today = utils.getDay(0),
      getListParams = {
        "deviceId" : that.data.deviceId,
        "beginTime" : parseInt(threeDay.replace(/-/g, '')),
        "endTime" : parseInt(today.replace(/-/g, ''))
      };
      let datalist = [], timeList = [];
     utils.promiseGet('/wx/device/spray/getEleOpenTimesByDay', getListParams).then(res => {
      console.log(res);
      if (res.code == 200) {
        let result = res.data;
        if (result.list.length > 0) {
          for (let i = 0; i < result.list.length; i++) {
            console.log(result.list[i])
            datalist.push(result.list[i].eleOpenNum);
            timeList.push(result.list[i].uploadDate);
          }
        }
      } 
      if (datalist.length == 0) {
        datalist.push(0);
        timeList.push(today.replace(/-/g, ''));
      }
      that.initChartCount({
        list: [
          datalist
        ],
        timeList
      });
    }).catch(e => {
      console.log(e);
    });
  },

  getLineOption(params) {
    const option = {
      color: ['#E8564D'],
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: params.timeList
      },
      yAxis: [
        {
          type: 'value',
          name: '',
          min: 0,
          max: 100,
          position: 'left',
          axisLine: {
            show: true,
          },
          axisLabel: {
            formatter: '{value}'
          }
        }
      ],
      series: [{
        type: 'line',
        smooth: true,
        data: params.list[0],
        lineStyle: {
          width: 2
        },
        emphasis: {
          lineStyle: {
            width: 2
          }
        }
      },],
    };
    return option
  },

  initChartCount(data) {
    this.chartCmpCount = this.selectComponent('#chart-count')
    this.chartCmpCount.init((canvas, width, height, dpr) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr
      })
      chart.setOption(this.getLineOption(data))
      return chart
    })
  }
})